﻿aresult = " 通过||不通过||不通过但继续";
function onActivityResultDropped(e) {
    var $target = $(e.dropTarget);
    if ($target.is('ul.ws-activityresults')) {
        e.preventDefault();
        var nodetext = getTreeviewSelectedItemText($('#activityresults'), e);
        var $win = $('#Window').data('tWindow');
        var $newli = $target.addListItem(null);
        initialActivityResultLi($newli, nodetext);

    }
}

function initialActivityResultLi($newli, nodetext) {
    var $target = $('.ws-activityresults');
  
    $newli.val(nodetext).find('.display-text').text(nodetext);

    var data = $newli.data('lidata');
    if (data) {
        $newli.find('.display-text').text(nodetext + " : " + data.Activity.ActivityName + "结果为=>" + data.ResultString);
    }
    $newli.addHoverFunctions({
        deleteEvent: function () {
            $newli.remove();
        },
        detailEvent: function () {
            setDetailToWindowEditor($newli, initialActivityResultDetail($newli), $('#Window'));
        }
    });
}

function initialActivityResultDetail($target) {

    var collection = $('.ws-activities').find('li');
    var options = "";
    collection.each(function () {
        var data = $(this).data('lidata');
        if (options == "")
            options = data.ActivityName;
        else
            options += "||" + data.ActivityName;
    });

  

    var data = $target.data('lidata');
    var array = new Array();
    array.push({ controltype: 'text', fieldname: '结果名称', fieldvalue: data == null ? null : data.Title, must: true });
    array.push({ controltype: 'dropdownlist', fieldname: '待选环节', fieldvalue: data == null ? null : data.Activity.ActivityName, fieldvaluereference: ".ws-activities li", optionalvalue: options, must: true });
    array.push({ controltype: 'radio', fieldname: '环节结果', fieldvalue: data == null ? null : data.ResultString, optionalvalue: aresult });
    $target.data('av', array);
    var win = $('#Window').data('tWindow');
    var $table = $target.getTable(
                {
                    save: function () {
                        if ($target.attr('sucess') == 'true') {
                            $target.find('.display-text').text(array[0].fieldvalue + ":" + array[1].fieldvalue.ActivityName + " " + array[2].fieldvalue);
                            $target.attr('uniquename', array[0].fieldvalue);
                            $target.removeClass('unsave').addClass('saved');
                            saveActivityResultData($target);
                            win.close();
                        }
                    },
                    cancel: function () {
                        win.close();
                    }
                });

              
    return $table;
}

function saveActivityResultData($li) {
    var av = $li.data('av');
    var lidata = { Title: av[0].fieldvalue, Activity: av[1].fieldvalue, ResultString: av[2].fieldvalue }
    $li.data('lidata', lidata);
}

function getActivityResultsData() {
    var array = new Array();
    $('ul.ws-activityresults').children('li').each(function () {
        var data = $(this).data('lidata');
        array.push({ WorkflowID: $("#workflowid").val(), ID: $(this).attr('id'), Title: data.Title, ActivityID: data.Activity.ID, Activity: data.Activity, ResultString: data.ResultString });
    });
    return array;
}

function setActivityResultData(data) {
    if (data) {
        var $ul = $('.ws-activityresults');
        for (var i = 0; i < data.length; i++) {
            var $li = $ul.addListItem({ lidata: data[i] });
            var uniquename = data[i].Title;
            $li.attr('id', data[i].ID);
            $li.attr('uniquename', uniquename);
            initialActivityResultLi($li, uniquename);
        }
    }
}